<?
include('../scaffold/comment.php');
include('../scaffold/content.php');
include('../scaffold/tag.php');
include('../controller/handler.php');
include('../controller/philomousia.php');
include('../controller/recaptcha.php');

include('../module/var.inc.php');

session_start();

$hd = new Handler();
$ph = new Philomousia();
$base_url = $hd->getBaseURL();
$hd->openDB();
// Current page number
$page = (isset($_GET['page']) && $_GET['page']>1)? $_GET['page'] : 1;
// Type of page (faculty|thought|paper) if any
$type = (isset($_GET['type']))? $_GET['type'] : -1;
// Tag of contents, if any
$tag_id = (isset($_GET['tag']))? $_GET['tag'] : -1;
// Relative URL to current page
$this_page = ($type!=-1)? $links[$type] : "";
// Relative URL for tag, if any
$tag_url = ($tag_id>0)? "tag:".$tag_id : "";
// Show navigation arrow in bottom page
$display_prev_next = true;
// Show comment form&container (only if we are in single-post-display mode, that is, id!=NULL)
$display_comment = false;
// Number of displayable content per page
$quantity = $hd->getContentsPerPage();
// Current offset
$offset = ($page-1)*$quantity;

if($hd->allowComments()) {
	$failed = false;
	/*if(isset($_POST['content'])) {
		if(($hd->useSecurityImage() && strtoupper($_POST['code'])==$_SESSION['code']) || !$hd->useSecurityImage()) {
			$ph->insertComment($_POST['content'], $_POST['author'], $_POST['body'], $hd);
		} else {
			$failed = true;
			$feedback = "Il codice inserito non corrisponde con quello indicato nell'immagine.";
		}
	}*/
	if(isset($_POST['content'])) {
		$resp = recaptcha_check_answer ($hd->getPrivateKey(),
										$_SERVER["REMOTE_ADDR"],
										$_POST["recaptcha_challenge_field"],
										$_POST["recaptcha_response_field"]);
		if(!$resp->is_valid) {
		  	$failed = true;
			$feedback = "Il codice inserito non corrisponde a quello indicato nell'immagine.";
		} else {
			$ph->insertComment($_POST['content'], $_POST['author'], $_POST['body'], $hd);
		}
	}
}

if(isset($_GET['id'])) {
	$contents_number = -1;
	$contents[] = $ph->loadContent($_GET['id'], $hd);
	$display_prev_next = false;
	$display_comment = true;
	$comments = $ph->loadComments($_GET['id'], $hd);
} else {
	$contents_number = $ph->getContentsNumber($hd, $type, $tag_id);
	$contents = $ph->loadContents($hd, $type, $offset, $quantity, $tag_id);
}
if($tag_id>0) {
	$tag = $ph->loadTag($tag_id, $hd);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
	<? include('../module/head.inc.php'); ?>
	<script src="<?echo $base_url;?>js/editor.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>
<div id="<?echo $links[$type];?>"></div>
<div id="content">
<?include('../module/header.inc.php');?>

<?
foreach($contents as $content) {
?>
<div class="article">
	<div class="upper_bar_main"><div class="upper_bar_round"></div></div>
	<div class="body_main_left">
	<div class="body_main_right">
	<div class="body_main">
	<h2><?echo $content->getTitle();?></h2>
	<div class="body">
		<div class="small date"> 
			<div class="month"><?echo strftime("%b", strtotime($content->getDate()));?></div> 
			<div class="day"><?echo strftime("%e", strtotime($content->getDate()));?></div> 
			<div class="year"><?echo strftime("%Y", strtotime($content->getDate()));?></div> 
		</div>
		<?echo $hd->makeHTML($content->getBody());?> &loz;
		<p class="author"><?echo $content->getAuthor();?></p>
	</div>
	
	<p class="small tags">
		<?if($type==-1) {?>Archiviato in <a href="<?echo $base_url.$links[$content->getType()];?>">
			<?echo $section[$content->getType()];?></a> | <?}?>
		Articoli simili in 
		<?foreach($content->getTags() as $key => $value) {?>
			<!--<a href="<?echo $base_url.$this_page."tag:".$value->getId();?>"><?echo $value->getName();?></a> -->
			<a href="<?echo $base_url."tag:".$value->getId();?>"><?echo $value->getName();?></a> 
		<?}?>
		<?if($hd->allowComments()) {?>
		|
		<?if(!isset($_GET['id'])) {?>
		<a href="<?echo $base_url.$links[$content->getType()];?>/post:<?echo $content->getId();?>">
			<?php echo $ph->getCommentsNumber($hd, $content->getId()) ?> commenti</a>
		<?} else {?>
			<?echo count($comments);?> commenti<!-- | 
			<span class="pseudo_link" onclick="new Effect.toggle('comment_form', 'blind')">Inserisci commento</span>-->
		<?}?>
		<?}?>
	</p>
	
	<?if($hd->getPassword()==sha1($_SESSION['password'])) {?>
	<p class="small admin_action">
		<a href="<?echo $base_url;?>panel">Pannello di amministrazione</a> |
		<a href="<?echo $base_url;?>panel/manage:<?echo $content->getId();?>">Gestisci articolo</a>
	</p>
	<?}?>
	</div></div></div>
	<div class="lower_bar_main"><div class="lower_bar_round"></div></div>
</div>
<?}?>

<div id="page">
<div class="upper_bar_main"><div class="upper_bar_round"></div></div>
<div class="body_main_left">
<div class="body_main_right">
<div class="body_main">
	<div id="page_body" class="small">
	<?if($contents_number>=0) {?><?echo $contents_number;?> articoli <?} else {?> Archiviato <?}?>
	<?if($tag_id>0) echo "di <a href=\"".$base_url.$tag_url."\">".$tag->getName()."</a>";?>
 	<?echo ($type!=-1)? "in <a href=\"".$base_url.$links[$type]."\">".$section[$type]."</a>" : "";?>
	<?if(($contents_number>$offset+$quantity || $page>1) && $display_prev_next) {?>
	<? include('../module/navigator.inc.php'); ?>
	<?}?>
	</div>
</div></div></div>
<div class="lower_bar_main"><div class="lower_bar_round"></div></div>
</div>

<?if($hd->allowComments() && $display_comment) { 
	include('../module/comment.inc.php');
}?>

<? include('../module/footer.inc.php'); ?>
</div>
<? include('../module/analytics.inc.php'); ?>
</body>
</html>

<?$hd->closeDB();?>